#include "Comm.hpp"
#include <unistd.h>

int main()
{
    Fifo fifo(Path);

    int rfd = open(Path, O_RDONLY);
    if(rfd < 0)// 打开失败
    {
        cout << "open failed, errno: " << errno << ", errstring: " << strerror(errno) <<  endl;
    }

    // 如果我们的写端没打开，先读打开fifo(先运行./pipe_server),open打开的时候就会阻塞
    // 直到把写端打开，此时读open才会返回
    cout << "open success" << endl;

    char buffer[1024];
    while(true)
    {
        ssize_t n = read(rfd, buffer, sizeof(buffer) - 1);
        if(n > 0)//读取成功
        {
            buffer[n] = 0;
            cout << "client say: " << buffer << endl;
        }
        else if(n == 0)
        {
            cout << "client quit, me too!!" << endl;
            break; 
        }
        else
        {
           cout << "raed failed, errno: " << errno << ", errstring: " << strerror(errno) <<  endl;
        }
    }
    return 0;
}

